#include "detail.h"
#include "ui_detail.h"
#include "../data/company.h"
#include "qradar.h"
#include <QMessageBox>


Detail::Detail(Company *company) :
    ui(new Ui::Detail) {
    qDebug()<<"Detail::Detail called.";
    ui->setupUi(this);
    setModal(false);
    /* TODO: [Done] 数据上屏
     * 数据：company
     */
    //bug2：两个表扩展到ui宽度
    ui->tableWidget_basicinfo->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_keyinfo->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    for(int i=0; i<ui->tableWidget_basicinfo->columnCount(); i++) {
        QTableWidgetItem *item=new QTableWidgetItem(company->tag->at(i));
        ui->tableWidget_basicinfo->setItem(0,i,item);
        //bug2：文字居中
        ui->tableWidget_basicinfo->item(0,i)->setTextAlignment(Qt::AlignCenter);
    }
    for(int i=0; i<4; i++)
        ui->tableWidget_basicinfo->horizontalHeader()->setSectionResizeMode(i, QHeaderView::ResizeToContents);     //然后设置要根据内容使用宽度的列
    ui->tableWidget_basicinfo->horizontalHeader()->setSectionResizeMode(6, QHeaderView::ResizeToContents);
    ui->tableWidget_basicinfo->horizontalHeader()->setSectionResizeMode(7, QHeaderView::ResizeToContents);
    for(int i=0; i<ui->tableWidget_keyinfo->columnCount(); i++) {
        QTableWidgetItem *item=new QTableWidgetItem(company->tag->at(i+ui->tableWidget_basicinfo->columnCount()));
        if(company->source!=nullptr && company->source->at(i)>0)
            item->setToolTip(QString("参与该标签的企业信息：%1条").arg(company->source->at(i)));
        ui->tableWidget_keyinfo->setItem(0,i,item);
        //bug2：文字居中
        ui->tableWidget_keyinfo->item(0,i)->setTextAlignment(Qt::AlignCenter);
    }
    /* TODO: [Done] 用QChart在graphicsView_radar上画出雷达图
     * https://github.com/dongshimou/qt-radar
     */
    //使用QMap存储8个标签对应的标签值
    QMap<QString,int> creditlevel;//信用水平
    QMap<QString,int> creditcrisis;//信用危机
    QMap<QString,int> capability;//经营能力
    QMap<QString,int> stablelevel;//稳定等级
    QMap<QString,int> enterpriseage;//企业年龄
    QMap<QString,int> enterprisescale;//企业规模
    QMap<QString,int> businessrisk;//经营风险
    QMap<QString,int> judicialrisk;//司法风险
    creditlevel["高"]=90,creditlevel["较高"]=75,creditlevel["一般"]=60,creditlevel["差"]=45;
    creditcrisis["一般"]=90,creditcrisis["较大"]=75,creditcrisis["大"]=60,creditcrisis["极大"]=45;
    capability["极强"]=95,capability["较强"]=85,capability["中等"]=75,capability["较差"]=65,capability["差"]=55;
    stablelevel["高"]=95,stablelevel["较高"]=85,stablelevel["一般"]=75,stablelevel["较低"]=65,stablelevel["低"]=55;
    enterpriseage["极长"]=90,enterpriseage["长"]=75,enterpriseage["较长"]=60,enterpriseage["中等"]=45;
    enterprisescale["较大"]=95,enterprisescale["中等"]=85,enterprisescale["较小"]=75,enterprisescale["微小"]=65,enterprisescale["小"]=55;
    businessrisk["一般"]=90,businessrisk["大"]=75,businessrisk["较大"]=60,businessrisk["极大"]=45;
    judicialrisk["一般"]=90,judicialrisk["大"]=75,judicialrisk["较大"]=60,judicialrisk["极大"]=45;
    QList<QString> radarTag;
    radarTag<<"信用水平"<<"信用危机"<<"经营能力"<<"稳定水平"
            <<"企业年龄"<<"企业规模"<<"经营风险"<<"司法风险";
    list<<creditlevel<<creditcrisis<<capability<<stablelevel
        <<enterpriseage<<enterprisescale<<businessrisk<<judicialrisk;
    if(company->entname==nullptr)
        ui->label_entname->setText("预测企业");
    else
        ui->label_entname->setText(*company->entname);

    QRadar *w=new QRadar(this);
    w->setStyleSheet(
        "QRadar{"
        "background-color:transparent;"
        "}"
    );
    for(int i=0; i<radarTag.length(); i++) {
        if(list.at(i).contains(company->tag->at(i+ui->tableWidget_basicinfo->columnCount())))
            w->addStat(radarTag.at(i),list.at(i)[company->tag->at(i+ui->tableWidget_basicinfo->columnCount())]);
    }
    w->configAxisColor(Qt::red);
    auto blue = QColor(66, 66, 255, 128);
    auto yellow = QColor(255, 255, 0, 128);
    w->configAreaColor(blue, yellow, Qt::red, Qt::red);
    w->getRadialAxis()->hide();
    w->drawChart();
    w->getCenterSeries()->hide();

    w->setObjectName(QString::fromUtf8("chartView_radar"));
    ui->verticalLayout->addWidget(w,1);
    w->setMinimumHeight(300);
    w->show();
    qDebug()<<"Detail::Detail returned.";
}

Detail::~Detail() {
    qDebug()<<"Detail::~Detail called.";
    delete ui;
    qDebug()<<"Detail::~Detail returned.";
}
